package com.ay;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.AcroFields.Item;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfNumber;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfString;

public class PdfFormUtils
{
	public static byte[] fillForm(InputStream is, Map<String, String> fieldValues) throws DocumentException, IOException
	{
		PdfReader reader = new PdfReader(is);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PdfStamper stamper = new PdfStamper(reader, baos);
		AcroFields fields = stamper.getAcroFields();
		BaseFont bfChinese = BaseFont.createFont("STSong-Light",
				"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

		for (String key : fieldValues.keySet())
		{
			fields.setFieldProperty(key, "textfont", bfChinese, null);
			fields.setField(key, fieldValues.get(key));
		}

		stamper.setFormFlattening(true);

		stamper.close();
		reader.close();
		return baos.toByteArray();
	}

	public static Map<String, Object> getPdfFormFields(InputStream is) throws IOException, DocumentException
	{
		PdfReader reader = new PdfReader(is);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PdfStamper stamper = new PdfStamper(reader, baos);
		AcroFields fields = stamper.getAcroFields();

		Map<String, Object> pdfFieldMap = new HashMap<String, Object>();
		Map<String, Item> fieldMap = fields.getFields();
		for (String name : fieldMap.keySet())
		{
			Item item = fields.getFieldItem(name);
			PdfDictionary mergedFieldDictionary = item.getMerged(0);
			PdfNumber maxLengthNumber = mergedFieldDictionary.getAsNumber(PdfName.MAXLEN);
			Integer maxLength = null;
			if (maxLengthNumber != null)
			{
				maxLength = maxLengthNumber.intValue();
			}
			System.out.println(name + "-maxlength: " + maxLength);
			
			PdfString pdfString = mergedFieldDictionary.getAsString(PdfName.TU);
			String tooltip = null;
			if (pdfString != null) {
				tooltip = pdfString.toUnicodeString();
			}
			System.out.println(name + "-tooltip: " + tooltip);
//			Set<PdfName> keys = mergedFieldDictionary.getKeys();
//			for (PdfName pdfName : keys)
//			{
//				if (pdfObject.isString()) {
//					System.out.println(mergedFieldDictionary.getAsString(pdfName).toUnicodeString());
//					System.out.println(mergedFieldDictionary.getAsString(pdfName).toUnicodeString());
//				}
//			}
			int fieldType = fields.getFieldType(name);
			String fileTypeString = null;
			switch (fieldType)
			{
			case AcroFields.FIELD_TYPE_CHECKBOX:
				fileTypeString = "Checkbox";
				break;
			case AcroFields.FIELD_TYPE_COMBO:
				fileTypeString = "Combobox";
				break;
			case AcroFields.FIELD_TYPE_LIST:
				fileTypeString = "List";
				break;
			case AcroFields.FIELD_TYPE_NONE:
				fileTypeString = "None";
				break;
			case AcroFields.FIELD_TYPE_PUSHBUTTON:
				fileTypeString = "Pushbutton";
				break;
			case AcroFields.FIELD_TYPE_RADIOBUTTON:
				fileTypeString = "Radiobutton";
				break;
			case AcroFields.FIELD_TYPE_SIGNATURE:
				fileTypeString = "Signature";
				break;
			case AcroFields.FIELD_TYPE_TEXT:
				fileTypeString = "Text";
				break;
			default:
				fileTypeString = "Unknown";
				break;
			}
			// String field = fields.getField(name);
			pdfFieldMap.put(name, fileTypeString);
		}
		return pdfFieldMap;
	}
	
	public static String getCheckboxValue(InputStream is, String name) throws IOException
	{
		PdfReader reader = new PdfReader(is);
		AcroFields fields = reader.getAcroFields();
		String[] values = fields.getAppearanceStates(name);
		StringBuffer sb = new StringBuffer();
		for (String value : values)
		{
			sb.append(value);
			sb.append('\n');
		}
		return sb.toString();
	}
}
